qx.Class.define("secretcuz.ui.BlogPostProfilePage", {
	extend : qx.ui.tabview.Page,

	construct : function() {
		this.base(arguments, "Compose", "secretcuz/icons/pen.png");
		this.setLayout(new qx.ui.layout.HBox(10));
		
		this.add(this.__createLeftVBox(), {flex : 0});
		this.add(this.__createRightVBox(), {flex : 1});
	},

	members : {
		profileData : null,
		_titleTextFiled: null,
		_ckEditor: null,
		
		__createLeftVBox : function() {
			var container = new qx.ui.container.Composite(new qx.ui.layout.VBox(10));
//			container.set({
//				width: 620
//			});
			var titleGroup = new qx.ui.container.Composite(new qx.ui.layout.HBox(10));
			var lblTitle = new qx.ui.basic.Label();
			lblTitle.set({
				rich: true,
				value: "<b style='background-color: #000; padding: 3px 15px; color: #fff; font-size: 16px;'>Title</b>"
			});
			this._titleTextFiled = new qx.ui.form.TextField();
			titleGroup.add(lblTitle);
			titleGroup.add(this._titleTextFiled, {
				flex : 1
			});

			this._ckEditor = new secretcuz.ui.widget.CkEditor("");
			
			this._ckEditor.setWidth(container.getWidth());
			this._ckEditor.setHeight(220);
			
			container.add(titleGroup);
			container.add(this._ckEditor);
			return container;
		},

		__createRightVBox : function() {
			var container = new qx.ui.container.Composite(new qx.ui.layout.VBox(10));
			var btnGroup = new qx.ui.container.Composite(new qx.ui.layout.HBox(10));
			
			var btnPublish = new qx.ui.form.Button("Post");
			this._ckEditor.linkTo(btnPublish);
			btnPublish.addListener("execute", function(e) {
				if (this._titleTextFiled.getValue() != null && this._ckEditor.getValue() != null) {
					var name = prompt("Let others know who are YOU?");
					if (name == null) {
						name = "Ano Kata";
					}
					var data = {
						title: this._titleTextFiled.getValue(),
						content: this._ckEditor.getValue(),
						date: new Date(),
						meta: name
					}
					var bpRest = new secretcuz.io.rest.BlogPostResource();
					bpRest.addListener("success", function(e) {
						var app = qx.core.Init.getApplication();
						app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #66CD00;'>Published a new post.</b>");
					}, this);
					bpRest.addListener("error", function(e) {
						var app = qx.core.Init.getApplication();
						app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #CC0000;'>Failed to publish the post.</b>");
					}, this);
					bpRest.insert({}, data); // JSON.stringify
				} else {
					var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #CC0000;'>Do nothing. Enter fields to post.</b>");
				}
			}, this);
			
			var btnSave = new qx.ui.form.Button("Update");
			this._ckEditor.linkTo(btnSave);
			btnSave.addListener("execute", function(e) {
				if (this.profileData != null && this.profileData.id != null) {
					this.profileData.title = this._titleTextFiled.getValue();
					this.profileData.content = this._ckEditor.getValue();
					var bpRest = new secretcuz.io.rest.BlogPostResource();
					bpRest.addListener("success", function(e) {
						var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #66CD00;'>Updated the post.</b>");
					}, this);
					bpRest.addListener("error", function(e) {
						var app = qx.core.Init.getApplication();
						app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #CC0000;'>Failed to update the post.</b>");
					}, this);
					bpRest.update({id: this.profileData.id}, {
						title: this.profileData.title,
						date: this.profileData.date,
						meta: this.profileData.meta,
						content: this.profileData.content
					});
				} else {
					var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #CC0000;'>Do nothing. Edit a post to update.</b>");
				}
			}, this);
			btnGroup.add(btnPublish);
			btnGroup.add(btnSave);

			var box1 = new qx.ui.groupbox.GroupBox("Date Options");
			box1.setLayout(new qx.ui.layout.VBox());
			var rdbDateAuto = new qx.ui.form.RadioButton("Auto");
			rdbDateAuto.setMarginLeft(9);
			var rdbDateCustom = new qx.ui.groupbox.RadioGroupBox("Custom");
			rdbDateCustom.getChildControl("legend").setMarginTop(0);
			rdbDateCustom.getChildControl("legend").setPaddingTop(0);
			rdbDateCustom.getChildControl("frame").setMarginTop(14);
			rdbDateCustom.getChildControl("frame").setDecorator(null);
			new qx.ui.form.RadioGroup(rdbDateAuto, rdbDateCustom);
			
			var chooser = new qx.ui.control.DateChooser();
			rdbDateCustom.setLayout(new qx.ui.layout.HBox());
			rdbDateCustom.add(chooser);
			
			box1.add(rdbDateAuto);
			box1.add(rdbDateCustom);

			container.add(btnGroup);
			container.add(box1);
			return container;
		},
		
		populate : function(id) {
			if (id != null && id != 0) {
				var bpRest = new secretcuz.io.rest.BlogPostResource();
				bpRest.addListener("success", function(e) {
					this.profileData = e.getData();
					this._titleTextFiled.setValue(this.profileData.title);
					this._ckEditor.setData(this.profileData.content);
				}, this);
				bpRest.addListener("error", function(e) {
					var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #CC0000;'>Failed to load profile.</b>");
				}, this);
				bpRest.get({id: id});
			}
		}
	}
});
